package com.topvision.common.model;

import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.topvision.common.enumeration.DeleteFlag;
import com.topvision.common.security.SecurityUtils;
import com.topvision.common.security.UserInfo;
import com.topvision.utils.misc.IdGenerator;
import org.hibernate.annotations.Type;
import org.hibernate.envers.Audited;
import org.springframework.data.annotation.CreatedBy;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedBy;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
import org.springframework.security.core.Authentication;

import javax.persistence.*;
import java.io.Serializable;
import java.time.ZonedDateTime;

/**
 * @author shengwm
 * @version V0.0.1
 * @Title: DataEntity
 * @Package com.topvision.common.dto
 * @Description: 审计基类
 * @date 2017/4/18 13:52
 */
@MappedSuperclass
@Audited
@EntityListeners(AuditingEntityListener.class)
public abstract class DataEntity extends BaseEntity implements Serializable {
    private static final long serialVersionUID = -5519222682984900319L;

    @CreatedBy
    @JsonIgnore
    @Column(name = "created_by")
    protected String createdBy; // 创建者

    @CreatedDate
    @JsonIgnore
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @Column(name = "created_date")
    protected ZonedDateTime createdDate = ZonedDateTime.now(); // 创建日期

    @LastModifiedBy
    @JsonIgnore
    @Column(name = "updated_by")
    protected String updatedBy; // 更新者

    @LastModifiedDate
    @JsonIgnore
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @Column(name = "updated_date")
    protected ZonedDateTime updatedDate = ZonedDateTime.now(); // 更新日期

    /*@JsonIgnore
    @Column(name = "del_flag")
    protected DeleteFlag delFlag; // 删除标记（0：正常；1：删除；）*/

    @PrePersist
    public void prePersist() {
      //  this.delFlag = DeleteFlag.NORMAL;
        this.id = IdGenerator.getId();
        Authentication userInfo = SecurityUtils.getCurrentUserInfo();
       this.createdBy = (null == userInfo.getName() ? "" : userInfo.getName());
        this.updatedBy = createdBy;
    }

    public void prePersistNoId() {
       // this.delFlag = DeleteFlag.NORMAL;
        Authentication userInfo = SecurityUtils.getCurrentUserInfo();
        this.createdBy = (null == userInfo.getName() ? "" : userInfo.getName());
        this.updatedBy = createdBy;
    }

    @PreUpdate
    public void PreUpdate() {
        this.updatedDate = ZonedDateTime.now();
     //   UserInfo userInfo = SecurityUtils.getCurrentUserInfo();
       // this.updatedBy = (null == userInfo.getId() ? "" : userInfo.getId().toString());
        Authentication userInfo = SecurityUtils.getCurrentUserInfo();
        this.updatedBy = (null == userInfo.getName() ? "" : userInfo.getName());
    }

    public DataEntity() {
        super();
      //  this.delFlag = DeleteFlag.NORMAL;
    }

    public DataEntity(Long id) {
        super(id);
    }

    public String getCreatedBy() {
        return createdBy;
    }

    public void setCreatedBy(String createdBy) {
        this.createdBy = createdBy;
    }

    public ZonedDateTime getCreatedDate() {
        return createdDate;
    }

    public void setCreatedDate(ZonedDateTime createdDate) {
        this.createdDate = createdDate;
    }

    public String getUpdatedBy() {
        return updatedBy;
    }

    public void setUpdatedBy(String updatedBy) {
        this.updatedBy = updatedBy;
    }

    public ZonedDateTime getUpdatedDate() {
        return updatedDate;
    }

    public void setUpdatedDate(ZonedDateTime updatedDate) {
        this.updatedDate = updatedDate;
    }

    /*public DeleteFlag getDelFlag() {
        return delFlag;
    }

    public void setDelFlag(DeleteFlag delFlag) {
        this.delFlag = delFlag;
    }*/
}